package com.mallcai.bigdata.ladon.service.report.node;

import com.mallcai.bigdata.ladon.service.report.impl.ReportQueryParamHolder;
import com.mallcai.bigdata.ladon.utils.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * 复合型sql节点,该节点包含0~N个ReportSqlNode
 * @author feixy
 * @version 1.0
 * @date 2019-08-03 16:00
 */
public class CompositeReportSqlNode implements ReportSqlNode{

    private final List<ReportSqlNode> sqlNodeList;

    public CompositeReportSqlNode(List<String> sqls) {
        if (CollectionUtils.isEmpty(sqls)){
            sqlNodeList = Collections.emptyList();
        } else {
            sqlNodeList = CollectionUtils.filterAndMap(sqls, ReportSqlNodeFactory::create, StringUtils::isNotEmpty);
        }
    }

    @Override
    public String toSql(Map<String, ReportQueryParamHolder> params) {
        return CollectionUtils.join(sqlNodeList, reportSqlNode -> reportSqlNode.toSql(params), " ");
    }
}
